﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Paam01")]
    public class Paam01Controller : ApiController
    {   

        [HttpPost]
        [Route("PostPaam01_inq")]

        //多记录查询
        public System.Data.DataSet PostPaam01_inq([FromBody]  JObject obj)
        {

          

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();

           

            inBlock.Tables[0].Columns.Add("COMPANY_CODE");
            inBlock.Tables[0].Columns.Add("PURCHASE_APPLY_BILL_NO");
            inBlock.Tables[0].Columns.Add("BRANCH_CODE");
            inBlock.Tables[0].Columns.Add("BILL_STATUS");

            inBlock.Tables[0].Columns.Add("REC_CREATOR");
            inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
            inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");





            inBlock.Tables[0].Rows.Add();

            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.userid     = obj["USERID"].ToString();
            inBlock.sys_info.username = obj["USERNAME"].ToString();

            inBlock.sys_info.formname   = obj["FORMNAME"].ToString();


            inBlock.Tables[0].Rows[0]["COMPANY_CODE"] = obj["COMPANY_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["PURCHASE_APPLY_BILL_NO"] = obj["PURCHASE_APPLY_BILL_NO"].ToString();
            inBlock.Tables[0].Rows[0]["BRANCH_CODE"] = obj["BRANCH_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["BILL_STATUS"] = obj["BILL_STATUS"].ToString();
            inBlock.Tables[0].Rows[0]["REC_CREATOR"] = obj["REC_CREATOR"].ToString();

            inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = obj["REC_CREATE_TIME"].ToString();
            inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = obj["REC_REVISE_TIME"].ToString();


            //仅查询最近最近2天的；不然数据太多，影响小程序效率；
            if (inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString().Trim() == "")
            {
                inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = DateTime.Now.AddMonths(-1).ToString("yyyyMMddHHmmss");
            }

            if (inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"].ToString().Trim() == "")
            {
                inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = DateTime.Now.AddDays(1).ToString("yyyyMMddHHmmss");
            }

          



            EI.EIInfo outBlock = EI.EITuxedo.CallService("paam01_inq", inBlock);

            //小代码转换；
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PA29", "PAA1", "PA30", "FIBR", "FIDP");
            DataTable dtTemp = null;

            EI.EIInfo outBlock_TEMP2 = EF.Utility.ExecQuery("select t.STOCK_NO CODE ,t.stock_name as CODE_DESC_1_CONTENT from tsimm01 t");
           
        

            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            {

                if (outBlock.Tables[0].Rows[i]["REMARK"].ToString() == "对外销售出库时自动生成")
                {
                    outBlock.Tables[0].Rows[i].Delete();
                    continue;
                }

                string APN = outBlock.Tables[0].Rows[i]["APN"].ToString();
                if (APN.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["PAA1"].Select("CODE  = '" + APN + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["APN"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                string APPLY_CLASSIFY = outBlock.Tables[0].Rows[i]["APPLY_CLASSIFY"].ToString();
                if (APPLY_CLASSIFY.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["PA29"].Select("CODE  = '" + APPLY_CLASSIFY + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["APPLY_CLASSIFY"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                string STOCK_NO = outBlock.Tables[0].Rows[i]["STOCK_NO"].ToString();
                if (STOCK_NO.Trim() != "")
                {
                    dtTemp = outBlock_TEMP2.Tables[0].Select("CODE  = '" + STOCK_NO + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["STOCK_NO"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }


              

                string BILL_STATUS = outBlock.Tables[0].Rows[i]["BILL_STATUS"].ToString();
                if (BILL_STATUS.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PA30"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                string branch_code = outBlock.Tables[0].Rows[i]["BRANCH_CODE"].ToString();
                if (branch_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + branch_code + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string dptment_code = outBlock.Tables[0].Rows[i]["DPTMENT_CODE"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + dptment_code + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                string apply_date = outBlock.Tables[0].Rows[i]["APPLY_DATE"].ToString();
                if (apply_date.Length>=8)
                outBlock.Tables[0].Rows[i]["APPLY_DATE"] = apply_date.Substring(0, 4) + "-" + apply_date.Substring(4, 2) + "-" + apply_date.Substring(6, 2);



                string DELIVY_EXPECTED_DATE = outBlock.Tables[0].Rows[i]["DELIVY_EXPECTED_DATE"].ToString();
                if (DELIVY_EXPECTED_DATE.Length >= 8)
                    outBlock.Tables[0].Rows[i]["DELIVY_EXPECTED_DATE"] = DELIVY_EXPECTED_DATE.Substring(0, 4) + "-" + DELIVY_EXPECTED_DATE.Substring(4, 2) + "-" + DELIVY_EXPECTED_DATE.Substring(6, 2);


                



            }

            
            
            outBlock.Tables[0].TableName = "Table0";


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


        [HttpPost]
        [Route("PostPaam01_Info")]

        //单记录查询
        public System.Data.DataSet PostPaam01_Info([FromBody]  JObject obj)
        {

             EI.EIInfo inBlock = new EI.EIInfo();
           

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            string purchase_apply_bill_no = obj["PURCHASE_APPLY_BILL_NO"].ToString();

           

            //1* 采购申请单主项 + 子项查询*********************************************************
         

            inBlock.Tables[0].Columns.Add("PURCHASE_APPLY_BILL_NO");
            inBlock.Tables[0].Rows.Add();

            inBlock.Tables[0].Rows[0]["PURCHASE_APPLY_BILL_NO"] = purchase_apply_bill_no;

            inBlock.sys_info.company_code = "LT";

            



            inBlock.sys_info.userid     = obj["USERID"] == null ? " ": obj["USERID"].ToString();
            inBlock.sys_info.username   = obj["USERNAME"].ToString();
            inBlock.sys_info.formname   = "PAAM10_DD";
           
            EI.EIInfo outBlock = EI.EITuxedo.CallService("paam0102_inq", inBlock);

           
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PAOM","PA09","PO30","PA30", "FIBR", "FIDP", "PAA1", "PA29");

            EI.EIInfo outBlock_TEMP2 = EF.Utility.ExecQuery("select t.STOCK_NO CODE ,t.stock_name as CODE_DESC_1_CONTENT from tsimm01 t");
            //

           

            DataTable dtTemp = null;

            //主项代码转换；
            if (outBlock.Tables[0].Rows.Count == 1)
            {
                string branch_code = outBlock.Tables[0].Rows[0]["BRANCH_CODE"].ToString();
                if (branch_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + branch_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string dptment_code = outBlock.Tables[0].Rows[0]["DPTMENT_CODE"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + dptment_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string bill_status = outBlock.Tables[0].Rows[0]["BILL_STATUS"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PA30"].Select("CODE  = '" + bill_status + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                //申请分类
                string apply_classify = outBlock.Tables[0].Rows[0]["APPLY_CLASSIFY"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PA29"].Select("CODE  = '" + apply_classify + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["APPLY_CLASSIFY"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                //类型
                string apn = outBlock.Tables[0].Rows[0]["APN"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAA1"].Select("CODE  = '" + apn + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["APN"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string stock_no = outBlock.Tables[0].Rows[0]["STOCK_NO"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP2.Tables[0].Select("CODE  = '" + stock_no + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["STOCK_NO"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string apply_date = outBlock.Tables[0].Rows[0]["APPLY_DATE"].ToString();
                if (apply_date.Length >= 8)
                    outBlock.Tables[0].Rows[0]["APPLY_DATE"] = apply_date.Substring(0, 4) + "-" + apply_date.Substring(4, 2) + "-" + apply_date.Substring(6, 2);

                string delivy_expected_date = outBlock.Tables[0].Rows[0]["DELIVY_EXPECTED_DATE"].ToString();
                if (delivy_expected_date.Length >= 8)
                    outBlock.Tables[0].Rows[0]["DELIVY_EXPECTED_DATE"] = delivy_expected_date.Substring(0, 4) + "-" + delivy_expected_date.Substring(4, 2) + "-" + delivy_expected_date.Substring(6, 2);

                string rec_create_time = outBlock.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString();
                if (rec_create_time.Length >= 12)
                    outBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = rec_create_time.Substring(0, 4) + "-" + rec_create_time.Substring(4, 2) + "-" + rec_create_time.Substring(6, 2) + " " + rec_create_time.Substring(8, 2) + ":" + rec_create_time.Substring(10, 2);



            }



           




            //1跟踪履历***************************************************************************************
            string sql_str = " select  ";
            sql_str  +=" to_char(to_date(rec_create_time,'yyyymmddhh24miss'),'yyyy-mm-dd') REC_CREATE_TIME ";
            sql_str  +=" ,REC_CREATOR,REMARK1 ";
            sql_str += " from tpabm29 t where  ROW_GUID='" + purchase_apply_bill_no + "'";
            sql_str += " order by t.REC_CREATE_TIME desc  ";
            EI.EIInfo outBlock1 = EF.Utility.ExecQuery(sql_str);





         

            //2-采购合同情况***************************************************************************************附件
            sql_str = " select ta.APPLY_PERSON_NO, ta.PO_BILL_NO,ta.BILL_STATUS ,ta.CONT_SIGN_DATE,b.user_cnname, ";
            sql_str     +=  "  tb.ASMAT_NO,tb.ASMAT_NAME,tb.QUANTITY ,tb.UNIT,tb.DELIVY_EXPECTED_DATE  from TPAOM01 ta  ";
            sql_str     +=  "   left join (select user_code,user_cnname from tsosu01 where DISABLE_FLAG != '1') b   ";
			sql_str     +=  "  on ta.supplier_code = b.user_code    ";
            sql_str     +=  "   , TPAOM02 tb ";
            sql_str     +=  "  where  tb.purchase_apply_bill_no ='" + purchase_apply_bill_no + "' ";
            sql_str     +=  "  and ta.po_bill_no = tb.po_bill_no ";

            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(sql_str);

  
           
            dtTemp = null;

            //主项代码转换；
            for (int i = outBlock2.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                //单据状态
                string BILL_STATUS = outBlock2.Tables[0].Rows[i]["BILL_STATUS"].ToString().Trim();
                if (BILL_STATUS.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAOM"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock2.Tables[0].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                //签约日期
                string CONT_SIGN_DATE = outBlock2.Tables[0].Rows[i]["CONT_SIGN_DATE"].ToString();
                if (CONT_SIGN_DATE.Length >= 8)
                    outBlock2.Tables[0].Rows[i]["CONT_SIGN_DATE"] = CONT_SIGN_DATE.Substring(0, 4) 
                        + "-" + CONT_SIGN_DATE.Substring(4, 2) 
                        + "-" + CONT_SIGN_DATE.Substring(6, 2);
            }


           


              //3 收货情况***************************************************************************************执行结果
               sql_str  = "  select PA_RECEIPT_NO,BILL_STATUS,ASMAT_NO,ASMAT_NAME   ";
               sql_str += " ,SPEC,UNIT,SUPPLIER_NAME ,QUANTITY   ";
               sql_str += " ,CREATE_TIME,PA_CHECKOUT_NAME   ";
               sql_str += "  from tpasi07 a   ";
               sql_str += " where purchase_apply_bill_no ='" + purchase_apply_bill_no + "'";

               EI.EIInfo outBlock3 = EF.Utility.ExecQuery(sql_str);

             
               dtTemp = null;

               //主项代码转换；
               for (int i = outBlock3.Tables[0].Rows.Count - 1; i >= 0; i--)
               {
                   //单据状态
                   string BILL_STATUS = outBlock3.Tables[0].Rows[i]["BILL_STATUS"].ToString().Trim();
                   if (BILL_STATUS.Trim() != "")
                       dtTemp = outBlock_TEMP.Tables["PA09"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();

                   if (dtTemp != null && dtTemp.Rows.Count == 1)
                       outBlock3.Tables[0].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                   

                   //收货时间
                   string CREATE_TIME = outBlock3.Tables[0].Rows[i]["CREATE_TIME"].ToString();
                   if (CREATE_TIME.Length >= 12)
                       outBlock3.Tables[0].Rows[i]["CREATE_TIME"] = CREATE_TIME.Substring(4, 2)
                           + "-" + CREATE_TIME.Substring(6, 2)
                           + " " + CREATE_TIME.Substring(8, 2)
                           + ":" + CREATE_TIME.Substring(10, 2)
                           ;
                }
             

            outBlock.Tables[0].TableName = "T0";
            outBlock.Tables[1].TableName = "T1";

            outBlock1.Tables[0].TableName = "T29";
            outBlock2.Tables[0].TableName = "T02";
            outBlock3.Tables[0].TableName = "T07";
             

            outBlock.Tables.Add(outBlock1.Tables[0].Copy());
            outBlock.Tables.Add(outBlock2.Tables[0].Copy());
            outBlock.Tables.Add(outBlock3.Tables[0].Copy());



            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }



        [HttpPost]
        [Route("PostPaam01_Load")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        public System.Data.DataSet PostPaam01_Load([FromBody]  JObject obj)
        {           

            //小代码转换；
            EI.EIInfo outBlock = EF.Utility.GetCodeClassValue("PA30");

            DataRow dr1 = outBlock.Tables["PA30"].NewRow();
            dr1["CODE"] = " ";
            dr1["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["PA30"].Rows.InsertAt(dr1, 0);


          

            EI.EIInfo inBlock = new EI.EIInfo();
            EI.EIInfo outBlock1 = EI.EITuxedo.CallService("pabm05_inq2", inBlock);


            outBlock1.Tables[0].Columns[0].ColumnName = "CODE";
            outBlock1.Tables[0].Columns[1].ColumnName = "CODE_DESC_1_CONTENT";
           
            DataRow dr2 = outBlock1.Tables[0].NewRow();
            dr2["CODE"] = " ";
            dr2["CODE_DESC_1_CONTENT"] = " ";
            outBlock1.Tables[0].Rows.InsertAt(dr2, 0);
            
            outBlock1.Tables[0].TableName = "COMP";

            outBlock.Tables.Add(outBlock1.Tables[0].Copy());
 

            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        //[HttpPost]
        //[Route("PostPaam01_pro")]
 
    }
}